﻿Option Strict On
Option Explicit On

Imports System
Imports System.Linq
Imports System.Collections.Generic

<Flags()> _
Public Enum StandardImports
   None = 0
   System = 1
   SystemCollectionsGeneric = 2
   SystemLinq = 4
   SystemXmlLinq = 8
   SystemCollectionsObjectModel = 16
   SystemDataCommon = 32
   SystemData = 64
   CslaData = 128
   Csla = 256
   CslaValidation = 512

   ' Combos
   MinimalGroup = StandardImports.System Or _
                  StandardImports.SystemCollectionsGeneric

   CslaGroup = MinimalGroup Or _
               StandardImports.SystemCollectionsObjectModel Or _
               StandardImports.SystemData Or _
               StandardImports.SystemDataCommon Or _
               StandardImports.CslaData Or _
               StandardImports.Csla Or _
               StandardImports.CslaValidation

End Enum

Namespace VB
   Public Module StandardImportsExtensions
      <System.Runtime.CompilerServices.Extension()> _
      Public Function Output(ByVal standardImports As StandardImports) As String
         Dim ret As String = String.Empty
         Dim importStrings = GetImportListByVal(standardImports)
         For Each importString In importStrings
            ret &= "Imports " & importString & Strings.nl
         Next
         Return ret
      End Function

      Friend Function GetImportListByVal(ByVal standardImports As StandardImports) As List(Of String)
         ' We cannot use ToString because we expect common combos to be named
         Dim ret = New list(Of String)
         Dim valuesAsObject = System.Enum.GetValues(GetType(StandardImports))
         Dim values = From value In valuesAsObject _
                      Select CInt(value)
         For Each value In values.Where(Function(inValue) inValue <> 0)
            Select Case value
               Case StandardImports.None, StandardImports.MinimalGroup, StandardImports.CslaGroup
                  ' Do nothing
               Case StandardImports.SystemCollectionsObjectModel
                  ret.Add("System.Collections.ObjectModel")
               Case Else
                  If (standardImports And value) = value Or value = standardImports.System Then
                     ret.Add(System.Enum.GetName(GetType(StandardImports), value).DotsAtCaps)
                  End If
            End Select
         Next
         Return ret
      End Function
   End Module

End Namespace

Namespace CSharp
   Public Module StandardImportsExtensions
      <System.Runtime.CompilerServices.Extension()> _
      Public Function Output(ByVal standardImports As StandardImports) As String
         ' We cannot use ToString because we expect common combos to be named
         Dim ret As String = String.Empty
         Dim importStrings = VB.GetImportListByVal(standardImports)
         For Each importString In importStrings
            ret &= "using " & importString & ";" & Strings.nl
         Next
         Return ret
      End Function
   End Module
End Namespace